package com.lance.answer.question_answer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lance.answer.dto.CompleteInfo;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.DealInfoUtil;
import com.lance.answer.util.Constant;
import com.lance.answer.util.HttpClientUtil;
import com.lance.answer.util.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.util.CollectionUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

@Slf4j
public class QuestionAnswer {

    /**
     * 进度表：
     * 6-18 19:55开始测试 公共行政学 行政管理专科
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        while (LocalDateTime.now().getHour() >= 3 && LocalDateTime.now().getHour() < 5) {
            log.info("当前时间：{} 国开网维护中 程序暂时休眠20min。。。", LocalDateTime.now().toString());
            HttpClientUtil.send("当前时间：" + LocalDateTime.now().toString() + "国开网维护中 程序暂时休眠20min....");
            TimeUnit.MINUTES.sleep(20);
        }
        //     HttpClientUtil.send("当前时间：" + LocalDateTime.now().toString() + "国开网维护结束 程序恢复运行....");

        //并发开启的浏览器数量
        int sync = 9;
        //是否提交答案
        boolean commit = true;

        //学科中文名
        //    String subjectName = "管理学基础";
//        String subjectName = "网络实用技术基础";

        //  String subjectName = "人力资源管理";
        //    String subjectName = "行政组织学";
        //  String subjectName = "社会调查研究与方法";
        //  String subjectName = "办公室管理";
        //   String subjectName = "个人与团队管理";
        //  String subjectName = "政治学原理";
        //     String subjectName = "公共行政学";  // 属于补课的课程。。
        //   String majorName = "行政管理专科20秋";


        // 1、https://wenku.baidu.com/view/c756c6e40266f5335a8102d276a20029bc646308.html
        // 2、https://wenku.baidu.com/view/5f4be0dc846fb84ae45c3b3567ec102de3bddf58.html?fixfr=hWxoFNOwy7JUEvCizReIYQ%253D%253D&fr=income1-wk_go_search-search
        // 3、https://wenku.baidu.com/view/481955925222aaea998fcc22bcd126fff7055da6.html
        // 4、https://wenku.baidu.com/view/a50394260b75f46527d3240c844769eae109a330.html?fixfr=VYaAbiUcjU86JNJkR0gXFw%253D%253D&fr=income7-wk_go_searchX-search

        // 基础写作 √
//            String subjectName = "建筑构造"; ///FIXME 答案不全
//         //   String subjectName = "建筑工程项目招投标与合同管理"; ///FIXME 等答案
//     //       String subjectName = "建筑测量"; //DONE
//       //     String subjectName = "建设法规"; //DONE
//        //    String subjectName = "建筑工程计量与计价";  //DONE
//          //  String subjectName = "建筑施工技术"; //DONE
//         String majorName = "建设工程管理20秋";


        //    String subjectName = "人力资源管理"; //TODO //DONE
        //   String subjectName = "公共行政学"; //TODO 形考一、二是话题分析 //DONE
        //    String subjectName = "政治学原理"; //TODO 形考一、二是话题分析 //DONE
        // String subjectName = "管理学基础"; //TODO //DONE
        //   String majorName = "行政管理专科2103";


        //   String subjectName = "建筑测量"; ///DONE
        //  String subjectName = "建筑工程项目管理"; // FIXME 线下完成的
        //     String subjectName = "建筑工程质量检验";   //TODO   //DONE
        //   String subjectName = "建筑施工技术"; //TODO  //DONE
//             String subjectName = "建设法规"; // //TODO  //DONE
        //    String subjectName = "建筑工程计量与计价";  //TODO //DONE
        //         String majorName = "建设工程管理专科2013秋";

        //   String subjectName = "形势与政策  //观看视频
        //  String subjectName = "西方经济学"; // 上传文件
        //     String subjectName = "管理英语1";   //TODO
        //   String subjectName = "建筑施工技术"; //TODO
        //  String subjectName = "基础会计"; // //TODO
        //       String subjectName = "毛泽东思想和中国特色社会主义理论体系概论";  //TODO
        //  String majorName = "工商企业管理专科2103";

        //  String subjectName = "行政领导学"; // //不在国开
        //    String subjectName = "公共政策概论"; //形考1-3上传  4做题 //DONE
        //  String subjectName = "当代中国政治制度"; //形考1话题 2-4做题
        // String subjectName = "中国近现代史纲要"; //
        // String subjectName = "马克思主义基本原理概论"; //
        // String subjectName = "形势与政策"; //
        // String subjectName = "管理英语3"; //
  //        String subjectName = "政府经济学"; ////形考1话题 2-4做题 FIXME 做题有问题！！！！！！！！10-20
     //      String majorName = "行政管理本科2103";

        //    String subjectName = "经济法学"; ////形考1话题 2-4做题 //FIXME 答案有问题 得去掉数字序号。 https://wenku.baidu.com/view/3d458255b80d4a7302768e9951e79b896902681c.html
        //   String subjectName = "行政法与行政诉讼法";  //DONE
        //   String subjectName = "刑事诉讼法学";  //DONE
        //    String majorName = "法律事务专科2103";


        // 还有商务英语1 西方经济学 DONE
        //   String subjectName = "★中级财务会计（一）"; //DONE
        //   String subjectName = "成本会计"; //DONE
//        String subjectName = "基础会计";  //DONE
//        String majorName = "会计专科2103";


        //      String subjectName = "药事管理与法规";  // https://www.docin.com/p-2608279035.html //DONE
        //     String subjectName = "病理学与病理生理学";  //DONE
        //   String subjectName = "医学免疫学与微生物学";  //DONE
        //  String subjectName = "人体解剖生理学";  //DONE
        //  String majorName = "药学专科2103";

        //  String subjectName = "微机系统与维护";  // 上传文件
        //   String subjectName = "Dreamweaver网页设计";  // 形考1，7,9是创建话题， 2 4 5 6 8 10上传文件  3做题
//        String subjectName = "网络实用技术基础";  // 1-4作业 5是上传文件
//      //  String subjectName = "理工英语1";  //
//      //  String subjectName = "";  //
        //      String majorName = "计算机信息管理专科2103";

        //      String subjectName = "管理心理学";  // TODO  //DONE
        //      String subjectName = "管理学基础";  // TODO //DONE
        //       String subjectName = "人力资源管理";  // TODO DONE
        //    String majorName = "人力资源管理专科2103"; //

        //       String subjectName = "可编程控制器应用";  // TODO DONE
        //    String subjectName = "机械制造基础";  // TODO  FIX ME 形考三有题目是上传文件  TODO 新增一个做题类型 答案就是本地文件上传得链接。
        //       String subjectName = "机械设计基础";  // fixme 线下？
        //      String subjectName = "电工电子技术";  // TODO DONE
        //    String majorName = "机电一体化技术专科2103"; //

       //     String subjectName = "学前儿童科学教育"; //TODO 等答案完善
       //    String majorName = "学前教育专科2103";

        //     String subjectName = "阅读与写作(1)"; // DONE
        //    String subjectName = "自然科学基础"; // 还有一个大作业是文件上传  几个创建话题和上传 DONE
        //        String subjectName = "人文社会科学基础(A)"; //  DONE
        //       String majorName = "小学教育专科2103";

      //   String subjectName = "西方经济学（本）"; // 有几个作业是上传文件的 /TODO
        //   String subjectName = "组织行为学"; //DONE
        //   String subjectName = "金融学"; 金融学怎么做？
    //      String subjectName = "公司概论"; //TODO
    //        String majorName = "工商管理本科2103";

        //     String subjectName = "国际私法"; //形考5是上传文件 还没做 TODO
        //    String subjectName = "国际公法"; //形考5也是上传文件 还没做  TODO
        //    String subjectName = "人文英语3"; // TODO!!! 得拉题！！！
     //      String subjectName = "劳动与社会保障法"; //形考二创建话题 得拉题
        // String subjectName = "中国法制史";  ？？国开没有作业
  //          String majorName = "法学本科2103";

//
    //    String subjectName = "国家开放大学学习指南";
    //   String majorName = "20春all";

//       String majorName = "20秋all";
//       String majorName = "20秋all";

          //     String subjectName = "形势与政策";
        //  String majorName = "20秋all";
        //  String majorName = "all";
        //       String majorName = "other1";
        //   String majorName = "alltmp";

       //     String subjectName = "计算机应用基础";
        //   String subjectName = "毛泽东思想和中国特色社会主义理论体系概论";
    //    String subjectName = "思想道德与法治";

   //     String majorName = "20秋all";
            // String majorName = "2103秋all";
         //    String majorName = "2103秋other";
       //     String majorName = "20秋alltmp";
        //  String majorName = "20秋all";


        //   String subjectName = "习近平新时代中国特色社会主义思想";
       //    String subjectName = "思想道德修养与法律基础";
           String subjectName = "毛泽东思想和中国特色社会主义理论体系概论";
        //   String majorName = "20秋all";
        //   String majorName = "20秋all";


        //是否推送微信消息
        boolean push = false;
        //驱动路径
        String driverPath = null;

        //初始化日志配置
        Constant.initLog();
        //开启任务
//        System.out.println("刷题课程：     " + subjectName);
//        System.out.println("刷题专业：     " + majorName);
//        System.out.println("并发开启浏览器：" + sync);
//        System.out.println("是否提交答案：  " + commit);
        StringBuilder sb = new StringBuilder();
        sb
                .append("刷题课程")
                .append(" : ")
                .append(subjectName)
                .append("\n")


                .append("并发开启浏览器")
                .append(" : ")
                .append(sync)
                .append("\n")

                .append("是否提交答案")
                .append(" : ")
                .append(commit)
                .append("\n");
        String message = sb.toString();
        System.out.println(message);
        if (push) {
            HttpClientUtil.send(message);
        }
        start(subjectName, sync, driverPath, commit, push);
    }

    public static void start(String subjectName, int sync, String driverPath, boolean commit, boolean push) throws Exception {
        //获取模板路径,通过流的方式获取文件可以避免被打成jar包之后无法获取文件路径的异常
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + subjectName + "-answer.json");
        String answerStr = IOUtils.toString(inputStream);

        List<AnswerObject> list = JSONObject.parseArray(answerStr, AnswerObject.class);
        Map<String, AnswerObject> map = new HashMap<>();
        for (AnswerObject answerObject : list) {
            map.put(answerObject.getQuestionCode(), answerObject);
        }


        InputStream inputStream3 = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + subjectName + "-quiz.section");
        List<String> list1 = IOUtils.readLines(inputStream3);
        List<String> paths = new ArrayList<>();
        for (String str : list1) {
            paths.add(str.split(",")[0].trim());
        }

        //  run(paths, map, subjectName, driverPath, userCode, pwd, commit, push);
        List<String> userNames = DealInfoUtil.countUserCourseInfo("E:\\gk\\userCourseInfo").get(subjectName);
        InputStream inputStream2 = new FileInputStream("E:\\gk\\allUser.txt");
        List<String> userString2 = IOUtils.readLines(inputStream2, "utf-8");

        Map<String, String> usernameAndPwd = Maps.newHashMap();
        for (String user : userString2) {
            String[] strs = user.split(",");
            String userCode = strs[0];
            String pwd = strs[1].substring(6, 14);
            pwd = "Ouchn" + pwd;
            usernameAndPwd.put(userCode, pwd);
        }
        List<List<String>> group = new ArrayList<>();
        int size = userNames.size();
        int step = size / sync;
        if (step == 0) {
            group.add(userNames);
        } else {
            for (int i = 0; i < sync; i++) {
                List<String> list2 = userNames.subList(step * i, (i + 1) * step);
                group.add(list2);
            }
            group.add(userNames.subList(step * sync, userNames.size()));
        }
        for (List<String> batchUser : group) {
            Constant.THREAD_POOL_BATCH.execute(() -> {
                try {
                    for (String user : batchUser) {
                        String userCode = user;
                        String pwd = usernameAndPwd.get(userCode);
                        //      String pwd = strs[1];
                        run(paths, map, subjectName, driverPath, userCode, pwd, commit, push);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

        }





    }


    // 管理学基础 2065001457555 19940419
    public static void run(List<String> urlTasks, Map<String, AnswerObject> answers, String subjectName, String driverPath, String userName, String password, boolean commit, boolean push) throws Exception {


        String filePath = "E:\\answerInfo\\" + subjectName;

        if (!new File(filePath).exists()) {
            new File(filePath).mkdirs();
        }

        filePath = filePath + "\\" + userName + ".txt";

        File completeInfoFile = new File(filePath);
        if (!completeInfoFile.exists()) {
            completeInfoFile.createNewFile();
        }
        InputStream inputStream = new FileInputStream(completeInfoFile);
        String completeInfoStr = IOUtils.toString(inputStream, "UTF-8");
        List<CompleteInfo> completeInfoList = JSONObject.parseArray(completeInfoStr, CompleteInfo.class);
        if (null == completeInfoList) {
            completeInfoList = Lists.newArrayList();
        }

        Set<String> haveCompleteHomeWorkUrlSet = Sets.newHashSet();
        for (CompleteInfo info : completeInfoList) {
            if (info.isComplete()) {
                haveCompleteHomeWorkUrlSet.add(info.getHomeworkUrl());
            }
        }

        // 定义是否开启浏览器没有这门课程
        boolean isStartChrome = false;

        for (String urlTask : urlTasks) {
            if (!haveCompleteHomeWorkUrlSet.contains(urlTask)) {
                isStartChrome = true;
                break;
            }
        }
        for (CompleteInfo info : completeInfoList) {
            if ("没有这门课程！".equals(info.getSummary())) {
                isStartChrome = false;
                break;
            }
        }

        if (!isStartChrome) {
            return;
        }

        WebDriver driver = Util.getDriver(driverPath);
        //String url = "http://student.ouchn.cn/";
        String url = "http://passport.ouchn.cn/Account/Login?ReturnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3Dstudentspace%26redirect_uri%3Dhttp%253A%252F%252Fstudent.ouchn.cn%252F%2523%252Fsignin-oidc%2523%26response_type%3Did_token%2520token%26scope%3Dopenid%2520profile%2520ouchnuser%2520ouchnstudentspaceapi%2520offline_access%26state%3Dca1cb5d0270b49fdaea508b8a846c457%26nonce%3Dd257df51b0a141ac8ed35b1caf59ef4c";
        driver.get(url);
        Thread.sleep(3000);
        //Util.login(driver, "2065001457555", "19940419");
        Util.login(driver, userName, password);
        Util.switchToFirst(driver);
        Thread.sleep(3000);
        // 形考作业的链接 管理学
        //List<String> urlTasks = Util.getCourseTaskUrl(driver, CourseTypeEnum.BasicManagement.name);
        //List<String> urlTasks = Util.getCourseTaskUrl(subjectCode);

        // 判断是否找到课程
        boolean isFindSubject = false;

        //chromeDriver.navigate().refresh();
        for (WebElement we : driver.findElements(By.className("media-body"))) {
            Util.switchToFirst(driver);
            // 找到管理学基础
            String mediaTitle = we.findElement(By.className("media-title")).getText();

            // 2021-12-10
//            if (!mediaTitle.contains(subjectName)) {
//                continue;
//            }
            // 2021-12-10

            if (!mediaTitle.equals(subjectName)) {
                continue;
            }

            isFindSubject = true;
            try {
                //点击进入学习的按钮
                WebElement studyButton = we.findElement(By.cssSelector("button[class='btn bg-primary']"));
                studyButton.click();
                //Util.switchToLast(driver);
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                File file = new File("d:\\user.txt");
                FileOutputStream outputStream = new FileOutputStream(file, true);
                //  String s= "用户："+userName+"毛概视频未刷\r\n";
                String s = userName + "," + password + "\r\n";
                outputStream.write(s.getBytes());
                try {
                    driver.close();
                    driver.quit();
                } catch (Exception e2) {

                }
            }
        }
        if (!isFindSubject) {
            try {
                CompleteInfo info = new CompleteInfo();
                info.setSummary("没有这门课程！");
                completeInfoList.add(info);
                FileUtils.write(completeInfoFile, JSONObject.toJSONString(completeInfoList, SerializerFeature.PrettyFormat), "utf-8");
                driver.close();
                driver.quit();
                return;
            } catch (Exception e) {

            }
        }

        System.out.println(urlTasks.toString());

        Map<Integer, Double> mmmm = Maps.newHashMap();
        mmmm.put(0, 20.0);
        mmmm.put(1, 20.0);
        mmmm.put(2, 20.0);
        mmmm.put(3, 20.0);
        mmmm.put(4, 20.0);


        int kkk = 0;
        for (String urlTask : urlTasks) {
            if (haveCompleteHomeWorkUrlSet.contains(urlTask)) {
                continue;
            }
            // FIXME 当完成第一份形考任务时，必须提交，否则会弹出 ”是否保存当前页面“消息
            driver.get(urlTask);
            // FIXME 延长了
            Thread.sleep(3 * 1000);
            // 点击继续作业

            boolean isGoOn = true;

            // cell c2
            CompleteInfo completeInfo = new CompleteInfo();
            try {
                List<WebElement> tdList = driver.findElements(By.cssSelector("td[class='cell c2']"));

                for (WebElement td : tdList) {
                    String score = td.getText();
                    //     System.err.println(score);
                    if ("还未评分".equals(score)) {
                        System.err.println("还未评分");
                        isGoOn = false;
                        break;
                    }
                    double d = Double.parseDouble(score);
                    if (d >=18.0) {
                        //       if (d >= mmmm.get(kkk)) {
                        completeInfo.setScore(score);
                        System.err.println("成绩已达到80.0分，跳过");
                        isGoOn = false;
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(2000);
            } finally {
                kkk++;
            }
            if (!isGoOn) {
                boolean hasGoOn = false;
                for (WebElement we : driver.findElements(By.cssSelector("button[class='btn btn-secondary']"))) {
                    if (!Strings.isNullOrEmpty(we.getText()) && "继续上次试答".equals(we.getText().trim())) {
                        hasGoOn = true;
                    }
                }
                if (!hasGoOn) {
                    completeInfo.setComplete(true);
                    completeInfo.setHomeworkUrl(urlTask);
                    completeInfoList.add(completeInfo);
                    continue;
                }
            }
            try {
                Thread.sleep(5000);
                WebElement doWorkArea = driver.findElement(By.cssSelector("div[class='box py-3 quizattempt']"));
                doWorkArea.findElement(By.cssSelector("button[class='btn btn-secondary']")).click();
            } catch (Exception e) {
                try {
                    System.err.println("taskUrl:" + urlTask + "  occur error");
                    e.printStackTrace();
                    driver.close();
                    driver.quit();
                } catch (Exception e1) {

                }
                return;
            }
            Thread.sleep(2 * 1000);
            //回到试题首页
            WebElement element = driver.findElement(By.id("quiznavbutton1"));
            element.click();
            Thread.sleep(2 * 1000);
            // 回到首页
            //Util.goHomePageIndex(driver);
            boolean hashNextPage = true;
            while (hashNextPage) {
                try {
                    Thread.sleep(4000);
                    // 单个页面的所有题目的集合
                    List<WebElement> singlePageQuestion = Util.getSinglePageQuestionList(driver);
                    // 遍历每个问题|题目


                    for (WebElement singleSubject : singlePageQuestion) {

                        WebElement contentElement = singleSubject.findElement(By.className("content"));

                        String title;
                        title = Util.getTitle(contentElement);
                        //兼容特殊字符
                        title = Util.dealContent(title);

                        String questionCode = MD5Utils.encode(subjectName + title);

                        if (!answers.containsKey(questionCode)) {
                            System.out.println("答案不存在");
                            System.out.println(title);
                            Map<String, String> map = new HashMap<>();
                            map.put("答案不存在", "未能从题库中匹配到");
                            map.put("答案标题", title);
                            map.put("课程名", subjectName);
                            //  Thread.sleep(5000000);
                            continue;
                        }

                        AnswerObject answerObject = answers.get(questionCode);

                        WebElement answerElement = Util.findByClassName(contentElement, "answer");
                        if (null != answerElement) {
                            // 开始答题
                            Util.doExercise(driver, answerElement, answerObject.getQuestionType(), answerObject.getAnswers());
                        } else {
                            // 如果为空就是填空题 是没有答案对比的
                            // 10-23 加了一个文件上传
                            Util.doExercise(driver, singleSubject, answerObject.getQuestionType(), answerObject.getAnswers());
                        }
                        //休眠一秒
                        TimeUnit.SECONDS.sleep(1);
                        //  FIXME 此时答案是没有保存的，要点击结束试答题
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    Thread.sleep(2000);
                    // FIX ME 获取当前页面的下一页信息 设置为while还是有下一页就继续循环
                    WebElement nextPage = Util.findNextPage(driver);
                    if (nextPage != null) {
                        nextPage.click();
                    } else {
                        hashNextPage = false;
                        // FIXME  此时答案是没有保存的，要点击结束试答题 当没有下一页的时候，可以选择提交
                    }
                }
            }


            Thread.sleep(2000);

            //回到试题首页 (保存最后一题答案)
           /* WebElement element2 = driver.findElement(By.id("quiznavbutton1"));
            element2.click();*/

            WebElement submitbtns = driver.findElement(By.className("submitbtns"));
            WebElement next = submitbtns.findElement(By.cssSelector("input[name='next']"));
            next.click();
            Thread.sleep(2000);

            if (!commit) {
                log.info("答案不提交");
                TimeUnit.SECONDS.sleep(2);
                continue;
            }

            //提交答案
            List<WebElement> saveButtons = driver.findElements(By.cssSelector("button[type='submit']"));
            //System.out.println("提交");
            //System.out.println(saveButtons.size());
            if (!CollectionUtils.isEmpty(saveButtons)) {
                for (WebElement webElement : saveButtons) {
                    String text = webElement.getText();
                    if (text.contains("提交")) {
                        webElement.click();
                    }
                }
            }

            TimeUnit.SECONDS.sleep(3);
            //全部提交
            //提交答案


            List<WebElement> confirmation = driver.findElements(By.className("confirmation-dialogue"));
            if (!CollectionUtils.isEmpty(confirmation)) {
                for (WebElement webElement : confirmation) {
                    List<WebElement> submitElement = webElement.findElements(By.tagName("input"));
                    if (!CollectionUtils.isEmpty(submitElement)) {
                        for (WebElement element1 : submitElement) {
                            String value = element1.getAttribute("value");
                            if (value.contains("提交")) {
                                //点击确认后页面会刷新,此时为了防止丢到元素抛异常，这里直接获取第一个，不再循环
                                element1.click();
                                break;
                            }

                        }
                    }
                }
            }


            List<WebElement> submitButtons = driver.findElements(By.tagName("input"));
            if (!CollectionUtils.isEmpty(submitButtons)) {
                for (WebElement webElement : submitButtons) {
                    String text = webElement.getText();
                    if (text.contains("提交")) {
                        webElement.click();
                    }
                }
            }

            TimeUnit.SECONDS.sleep(2);

            try {
                //判断是否符合要求
                Map<String, String> scoreMap = new HashMap<>();
                WebElement tbody = driver.findElement(By.tagName("tbody"));
                List<WebElement> trs = tbody.findElements(By.tagName("tr"));
                for (WebElement tr : trs) {
                    //th
                    WebElement th = tr.findElement(By.tagName("th"));
                    //td
                    WebElement td = tr.findElement(By.tagName("td"));
                    scoreMap.put(th.getText().trim(), td.getText().trim());
                }
                String score = scoreMap.get("分数");
                String[] strs = score.split("/");
                float s = Float.valueOf(strs[0]) / Float.valueOf(strs[1]);
                if (s < 0.80) {
                    scoreMap.put("科目", subjectName);
                    scoreMap.put("用户名", userName);
                    scoreMap.put("密码", password);
                    scoreMap.put("形考链接", urlTask);

                    if (push) {
                        HttpClientUtil.send(scoreMap);
                    }

                    System.out.println("答题不及格");
                    System.out.println(scoreMap.toString());
                } else {
                    completeInfo.setScore(score);
                    completeInfo.setComplete(true);
                    completeInfo.setHomeworkUrl(urlTask);
                    completeInfoList.add(completeInfo);
                }
            } catch (Exception e) {

            }

        }

        // 将新的完成作业的信息更新进去
        FileUtils.write(completeInfoFile, JSONObject.toJSONString(completeInfoList, SerializerFeature.PrettyFormat), "utf-8");

        Thread.sleep(5000);
        driver.close();
        driver.quit();
    }


}
